<?
function drizzo_login() {
	global $drizzo_users, $_POST;

	require_once('config.php');
	require_once('functions_db.php');	
	drizzo_db_connect();
	
	if ($_POST['login'] == "login") {
		$username = $_POST['username'];
		$password = $_POST['password'];
		$remember = $_POST['remember'];
		$password = md5($password);

		$result = @mysql_query("SELECT `username`,`password`,`ID` FROM `$drizzo_users` WHERE `username`='$username'");

		if (mysql_errno()) {		
			echo("<p>Error performing query: " . mysql_error() . "</p>");
		} else {
			while ($row = @mysql_fetch_assoc($result)) {
				$db_user = $row['username'];
				$db_pass = $row['password'];
				
				if (($db_user == $username) && ($db_pass == $password)) {
					$db_user = md5($row['username']);
					$db_pass = md5($row['password']);
					$domain = rtrim(str_replace(array('http://','www'),'',domain),'/');
					if(phpversion() >= 5.2) {
						if(!$remember) {
							setcookie('drizzo_user', $db_user, null, '/', $domain, 0, 1);
							setcookie('drizzo_pass', $db_pass, null, '/', $domain, 0, 1);
							header("Location: " . admin_folder . "");
						} else {
							$cookie_expire = time()+(365*24*60*60);
							setcookie('drizzo_user', $db_user, $cookie_expire, '/', $domain, 0, 1);
							setcookie('drizzo_pass', $db_pass, $cookie_expire, '/', $domain, 0, 1);
							header("Location: " . admin_folder . "");
						}
					} else {
						if(!$remember) {
							setcookie('drizzo_user', $db_user, null, '/', $domain, 0);
							setcookie('drizzo_pass', $db_pass, null, '/', $domain, 0);
							header("Location: " . admin_folder . "");
						} else {
							$cookie_expire = time()+(365*24*60*60);
							setcookie('drizzo_user', $db_user, $cookie_expire, '/', $domain, 0);
							setcookie('drizzo_pass', $db_pass, $cookie_expire, '/', $domain, 0);
							header("Location: " . admin_folder . "");
						}
					}
				}
			}
		}
	}
	

	if ($_POST['login'] == "logout") {
		setcookie('drizzo_user', '', time() - 3600, '/');
		setcookie('drizzo_pass', '', time() - 3600, '/');
		setcookie('drizzo_id', '', time() - 3600, '/');
		header("Location: " . admin_folder . "");
		exit();
	}
}

function drizzo_login_check() {
	if(!empty($_COOKIE['drizzo_user']) && !empty($_COOKIE['drizzo_pass'])) {
		return true;
	} else {
		return false;
	}
}
?>
